package com.yunhe.rentcars.pro.service.permi.impl;



import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.util.StringUtils;
import com.yunhe.rentcars.commons.util.mail.Mail;
import com.yunhe.rentcars.commons.util.mail.MailUtils;
import com.yunhe.rentcars.pro.dao.permi.UserDao;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.pro.entity.permi.User;
import com.yunhe.rentcars.pro.service.permi.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tianxuan
 * @version V1.0
 * @Date 2017/11/15 0015 上午 10:35
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

//    从spring中自动注入 需要的bean, 根据类型加载Bean  ，等价于  getBean(UserDao.class);
    @Autowired
    private UserDao userDao;

    @Log(value = "添加账户(t_account)",type = 1)
    public void save(User user) {
        userDao.save(user);
    }



    @Override
    public void delete(Long id) {
        userDao.delete(id);
    }


    @Log(value = "删除账户(t_account)",type = 2)
    @Override
    public void deleteBatch(String ids) {
        if(StringUtils.notEmpty(ids)){
            String[] idArr = ids.split(",");
            for(String i:idArr){
                this.delete(new Long(i));
            }
        }

    }

    @Log(value = "修改账户(t_account)",type = 3)
    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User findOne(String userName, String password) {
        User user1 = new User();
        user1.setUsername(userName);
        user1.setPassword(password);
        User user = userDao.findOne(user1);
        return user;
    }

    @Override
    public User find(User user) {
        User user1 = userDao.find(user);
        return user1;
    }

    @Override
    public List<User> findList() {
        List<User> userList = userDao.findList();
        return userList;
    }

    @Override
    public Pageable<User> findPage(Integer offset, Integer limit, User user) {

        int count = this.count(user);
        List<User> userList = null;
        if(count > 0){
            userList = userDao.findPage(offset, limit,user);
        }
        Pageable<User> pageable = new Pageable<>();
        pageable.setTotal(count);
        pageable.setRows(userList);
        return pageable;
    }

    @Override
    public int count(User user) {
        int count = userDao.count(user);
        return count;
    }

    @Override
    public Boolean findOne(User user) {


        User user1 = userDao.find(user);

        Boolean flag = false;
        if(null != user1){
            flag = true;
        }
        return flag;
    }

    @Override
    public String sendEmail(User user) {
        String msg = "邮件发送失败";
        if(null != user){
            String url = "http://localhost:8080/login";
            String contents = "已为您添加天禾汽车租赁系统用户账号，用户名："+user.getUsername()+"，密码："+user.getPassword()+"，请及时登录账号修改密码。<br><a href=\""+url+"\">点击此链接登录账号</a>";
            Mail mail = new Mail("2603459843@qq.com", user.getEmail(), "账号添加通知", contents);

            try {
                MailUtils.mail(mail);
                msg = "邮件发送成功";
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return msg;
    }

}
